<template>
  <div id="app">
    <header class="app-header">
      <div class="header-content">
        <h1>🔍 项目依赖关系分析工具</h1>
        <p class="header-subtitle">Vue3 + TypeScript 项目依赖可视化</p>
        <div v-if="currentFilePath" class="current-file-info">
          <el-tag :type="isLoading ? 'warning' : 'success'" size="small">
            {{ isLoading ? '🔄 加载中...' : `📄 ${shortFileName}` }}
          </el-tag>
        </div>
      </div>
    </header>

    <main class="app-main">
      <div class="container">
        <!-- 配置面板 -->
        <ConfigPanel @data-reloaded="handleDataReloaded" />

        <!-- 项目概览 -->
        <ProjectOverview
          ref="projectOverviewRef"
          :key="getComponentKey('project-overview')"
        />

        <!-- 统计卡片 -->
        <StatisticsCards
          ref="statisticsCardsRef"
          :key="getComponentKey('statistics-cards')"
        />

        <!-- 根文件依赖展示面板 -->
        <RootDependencyPanel
          ref="rootDependencyPanelRef"
          :key="getComponentKey('root-dependency-panel')"
        />

        <!-- 循环依赖详情面板 -->
        <CircularDependencyPanel
          ref="circularDependencyPanelRef"
          :key="getComponentKey('circular-dependency-panel')"
        />

        <!-- 图表展示区 -->
        <div class="charts-grid">
          <!-- 依赖类型分布饼图 -->
          <div class="chart-item">
            <DependencyPieChart :key="getComponentKey('dependency-pie-chart')" />
          </div>

          <!-- 包依赖关系图 -->
          <div class="chart-item full-width">
            <PackageGraphChart :key="getComponentKey('package-graph-chart')" />
          </div>
        </div>
      </div>
    </main>

    <!-- 调试面板（仅开发环境） -->
    <DebugPanel />

    <footer class="app-footer">
      <div class="footer-content">
        <p>&copy; 2025 项目依赖关系分析工具 - 基于 Vue3 + ECharts</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, type Ref } from 'vue';
import { useAnalysisStore } from './stores/analysis-store';
import ProjectOverview from './components/ProjectOverview.vue';
import StatisticsCards from './components/StatisticsCards.vue';
import DependencyPieChart from './components/DependencyPieChart.vue';
import PackageGraphChart from './components/PackageGraphChart.vue';
import RootDependencyPanel from './components/RootDependencyPanel.vue';
import CircularDependencyPanel from './components/CircularDependencyPanel.vue';
import ConfigPanel from './components/ConfigPanel.vue';
import DebugPanel from './components/DebugPanel.vue';

// 使用全局状态管理
const {
  getComponentKey,
  incrementDataVersion,
  currentFilePath,
  isLoading,
  shortFileName
} = useAnalysisStore();

// 组件引用
const projectOverviewRef: Ref<any> = ref(null);
const statisticsCardsRef: Ref<any> = ref(null);
const analysisPanelRef: Ref<any> = ref(null);
const rootDependencyPanelRef: Ref<any> = ref(null);
const circularDependencyPanelRef: Ref<any> = ref(null);

/**
 * 处理数据重新加载事件
 */
const handleDataReloaded = (): void => {
  console.log('🔄 处理数据重新加载事件，强制刷新所有组件');

  // 增加数据版本号，触发所有使用key的组件重新渲染
  incrementDataVersion();

  // 由于使用了key，组件会自动重新创建，不需要手动调用加载方法
  // 但为了兼容性，仍然保留手动触发逻辑
  setTimeout(() => {
    if (projectOverviewRef.value && typeof projectOverviewRef.value.loadOverviewData === 'function') {
      projectOverviewRef.value.loadOverviewData();
    }

    if (statisticsCardsRef.value && typeof statisticsCardsRef.value.loadStatisticsData === 'function') {
      statisticsCardsRef.value.loadStatisticsData();
    }

    if (analysisPanelRef.value && typeof analysisPanelRef.value.reload === 'function') {
      analysisPanelRef.value.reload();
    }

    if (rootDependencyPanelRef.value && typeof rootDependencyPanelRef.value.reload === 'function') {
      rootDependencyPanelRef.value.reload();
    }

    if (circularDependencyPanelRef.value && typeof circularDependencyPanelRef.value.reload === 'function') {
      circularDependencyPanelRef.value.reload();
    }
  }, 100); // 延迟执行确保组件已重新渲染
};
</script>

<style scoped>
#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.app-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding: 2rem 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  text-align: center;
}

.header-content h1 {
  color: white;
  font-size: 2.5rem;
  margin: 0 0 0.5rem 0;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.header-subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 1.1rem;
  margin: 0 0 1rem 0;
  font-weight: 400;
}

.current-file-info {
  margin-top: 0.5rem;
}

.current-file-info .el-tag {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  backdrop-filter: blur(5px);
}

.app-main {
  flex: 1;
  padding: 2rem 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-top: 20px;
}

.chart-item {
  min-height: 400px;
}

.chart-item.full-width {
  grid-column: 1 / -1;
}

.app-footer {
  background: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding: 1rem 0;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  text-align: center;
}

.footer-content p {
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  font-size: 0.9rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content h1 {
    font-size: 2rem;
  }

  .header-subtitle {
    font-size: 1rem;
  }

  .charts-grid {
    grid-template-columns: 1fr;
  }

  .container {
    padding: 0 15px;
  }
}
</style>
